<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>网络管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">

		function choose(selete)
		{
			var option=selete.options[selete.selectedIndex].value;
			if(option==0)
			{
				$("#subnet").show();

			}else{
				$("#subnet").hide();

			}

		}
		function addressChoose(selete)
		{
			var option=selete.options[selete.selectedIndex].value;
			if(option==1)
			{
				document.getElementById("ipVersion").value='4';

				$("#fixedAddressDiv").show();
				$("#poolAddressDiv").hide();

			}else{

				$("#fixedAddressDiv").hide();
				$("#poolAddressDiv").show();

			}

		}
		function checkValidate()
		{
			var type=document.getElementById("type").value;

			var networkName=document.getElementById("networkName").value;
			var subnetName=document.getElementById("subnetName").value;

			var selectObj=document.getElementById("subnetCreate");
			var isSubnet=selectObj.options[selectObj.selectedIndex].value;
			var subnetPoolObj=document.getElementById("subnetpoolId");
			var subnetPoolId=subnetPoolObj.options[subnetPoolObj.selectedIndex].value;

			var cidr=document.getElementById("cidr").value;
			var networkAddressSelectObj=document.getElementById("networkAddressSelect");
			var networkAddressSelectValue=networkAddressSelectObj.options[networkAddressSelectObj.selectedIndex].value;
			//判断是否为空
			if(networkName==null||networkName=="")
			{
				window.alertx("请输入网络名称！");
				return false;
			}
			if(type!='update')
			{
				if(isSubnet=='0')
				{
					if(subnetName==null||subnetName=="")
					{
						window.alertx("请输入子网名称！");
						return false;
					}
					if(networkAddressSelectValue=='1')
					{
						if(cidr=='')
						{
							window.alertx("请输入网络地址！");
							return false;
						}

					}else{
						if(subnetPoolId=='')
						{
							window.alertx("请选择一个子网池！");
							return false;
						}
					}




				}

			}

		}
		function chooseSubnetPool(subnetPools)
		{

			var id=subnetPools.options[subnetPools.selectedIndex].value;

//			alertx(document.getElementById(id).value);

			if(id!='')
			{
				var ipVersion=document.getElementById(id).value;
				document.getElementById("ipVersion").value=ipVersion;
				if(ipVersion=='4')
					document.getElementById("subnetPoolIpVersion").value="IPv4";
				else
					document.getElementById("subnetPoolIpVersion").value="IPv6";




				<%--$.ajax({--%>
				<%--type: "POST",--%>
				<%--url: "${ctx}/scene/sceneNetwork/getIPversion",--%>
				<%--data: {--%>
				<%--subnetPoolId:subnetPools.options[subnetPools.selectedIndex].value--%>
				<%--},--%>
				<%--success:function(ipVersion){--%>
				<%--document.getElementById("ipVersion").value=ipVersion;--%>
				<%--if(ipVersion=='4')--%>
				<%--document.getElementById("subnetPoolIpVersion").value="IPv4";--%>
				<%--else--%>
				<%--document.getElementById("subnetPoolIpVersion").value="IPv6";--%>

				<%--},--%>
				<%--error:function(){--%>
				<%--alertx("error");--%>
				<%--}--%>
				<%--});--%>
			}



		}
		function ipVersionSelectChoose(select)
		{

			var version=select.options[select.selectedIndex].value;
			document.getElementById("ipVersion").value=version;

		}
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});


	</script>
</head>
<body>
<ul class="nav nav-tabs">
	<li><a href="${ctx}/scene/sceneNetwork/">网络列表</a></li>
	<li class="active"><a href="${ctx}/scene/sceneNetwork/form?id=${sceneNetwork.id}">网络<shiro:hasPermission name="scene:sceneNetwork:edit">${not empty sceneNetwork.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="scene:sceneNetwork:edit">查看</shiro:lacksPermission></a></li>
</ul><br/>
<form id="inputForm" action="${ctx}/scene/sceneNetwork/save" method="post" class="form-horizontal">
	<sys:message content="${message}"/>
	<input type="hidden" id="id" name="id" value="${sceneNetwork.id}">
	<input type="hidden" id="type" name="type" value="${type}">

	<div class="control-group">
		<label class="control-label">网络名称：</label>
		<div class="controls">
			<input id="networkName" name="networkName" type="text" value="${sceneNetwork.networkName}" htmlEscape="false" maxlength="64" class="input-xlarge "/>
		</div>
	</div>
	<div class="control-group" <c:if test="${type!='update'}">hidden</c:if>>
		<label class="control-label">网络ID：</label>
		<div class="controls">
			<input type="text" htmlEscape="false" maxlength="64" class="input-xlarge " value="${sceneNetwork.id}" readonly/>
		</div>
	</div>
	<div <c:if test="${type=='update'}">hidden</c:if>>
		<div class="control-group">
			<label class="control-label">是否创建子网:</label>

			<div class="controls">
				<select id="subnetCreate" name="subnetCreate" onchange="choose(this)">
					<option value="0" selected>是</option>
					<option value="1">否</option>
				</select>
			</div>
		</div>
		<div id="subnet">
			<div class="control-group">
				<label class="control-label">子网名称：</label>
				<div class="controls">
					<input type="text" id="subnetName" name="subnetName" htmlEscape="false" maxlength="64" class="input-xlarge "/>
				</div>
			</div>
			<input type="hidden" value="4" id="ipVersion" name="ipVersion"/>
			<div class="control-group">
				<label class="control-label">网络地址资源:</label>

				<div class="controls">
					<select id="networkAddressSelect" name="networkAddressSelect" onchange="addressChoose(this)" class="input-xlarge">
						<option value="0" selected>从资源池分配网络地址</option>
						<option value="1">手动输入网络地址</option>
					</select>
				</div>
			</div>

			<div id="poolAddressDiv" hidden>
				<div class="control-group">
					<label class="control-label">地址资源池:</label>

					<div class="controls">
						<select id="subnetpoolId" name="subnetpoolId" onchange="chooseSubnetPool(this)" class="input-xlarge">
							<option value=""selected>===请选择===</option>
							<c:forEach items="${spList}" var="sp">
								<c:forEach items="${sp.spPrefixes}" var="spPrefix">
									<option value="${sp.spId}">${sp.spName}(${spPrefix})</option>
								</c:forEach>

							</c:forEach>
							<%--<option value="12345678">sp2(192.168.0.0/16)</option>--%>
							<%--<option value="23132s45">default-subnetpool(10.0.0.0/16)</option>--%>
						</select>
						<c:forEach items="${spList}" var="sp">
							<input type="hidden" id="${sp.spId}" value="${sp.spIpVersion}"/>
						</c:forEach>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label">IP版本：</label>
					<div class="controls">
						<input type="text" id="subnetPoolIpVersion" name="subnetPoolIpVersion" htmlEscape="false" maxlength="64" class="input-xlarge " value="IPv4" readonly>
					</div>
				</div>
			</div>
			<div id="fixedAddressDiv">
				<div class="control-group">
					<label class="control-label">网络地址：</label>
					<div class="controls">
						<input type="text" id="cidr" name="cidr" htmlEscape="false" maxlength="64" class="input-xlarge ">
					</div>
				</div>
				<div class="control-group">
					<label class="control-label">IP版本：</label>
					<div class="controls">
						<select id="ipVersionSelect" name="ipVersionSelect"  onchange="ipVersionSelectChoose(this)" class="input-xlarge">
							<option value="4" selected>IPv4</option>
							<option value="6">IPv6</option>
						</select>

					</div>
				</div>
			</div>

		</div>
	</div>

	<div class="form-actions">
		<shiro:hasPermission name="scene:sceneNetwork:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存" onclick=" return checkValidate()"/>&nbsp;</shiro:hasPermission>
		<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
	</div>
</form>
</body>
</html>